package com.jbp.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbp.dao.UserGetScoreRecordDao;
import com.jbp.exception.RRException;
import com.jbp.model.*;
import com.jbp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 积分领取记录(UserGetScoreRecord)表服务实现类
 * @author cc
 * @since 2021-12-07 19:56:57
 */
@Service
public class UserGetScoreRecordServiceImpl extends ServiceImpl<UserGetScoreRecordDao, UserGetScoreRecord> implements IService<UserGetScoreRecord> {
    @Autowired
    private UserService userService;
    @Autowired
    private PropertyService propertyService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private WalletRecordService walletRecordService;
    @Autowired
    private IdentifyService identifyService;
    @Autowired
    private UserDailySendgiftRecordService userDailySendgiftRecordService;
    @Autowired
    private UserDailyLuckRecordService userDailyLuckRecordService;

    @Autowired
    private AppConfigService appConfigService;

    /**是否领取过 ip 设备号  手机号*/
    public boolean hasGot(int userId) {
        UserGetScoreRecord record = new UserGetScoreRecord();
        User user = userService.getById(userId);
        record.setUserId(user.getId());
        record.setIp(user.getIp());
        record.setDevice(user.getDevice());
        record.setMobile(user.getMobile());

        Identify identify = identifyService.getByUserId(userId);
        if (identify!=null) {
//            record.setRealname(identify.getRealName());
            record.setIdno(identify.getIdNumber());
        }
        return getBaseMapper().hasGot(record)>0;
    }

    /**领取*/
    @Transactional
    public void got(int userId) {
        User user = userService.getById(userId);

        Identify identify = identifyService.getByUserId(userId);
        if (identify==null) {
            throw new RRException("请先实名认证");
        }

        if (user.getCreateType()!=0) {
            throw new RRException("小号不能领取");
        }

        boolean hasGot = hasGot(userId);
        if (hasGot) {
            throw new RRException("已经领过了");
        }
        Wallet wallet = walletService.getByUserId(userId);
        if (wallet.getScore().doubleValue()>0) {
            throw new RRException("已经领过了");
        }

        int on = appConfigService.getInt("score_get_on");

        if (on != 1) {
            throw new RRException("敬请期待");
        }
        //每天领取人数
        int  numPerDay = appConfigService.getInt("score_get_num_per_day");
        if (numPerDay <= getNumToday()){
            throw new RRException("已达今日领取上限");
        }

        //领取过大
        double num = appConfigService.getInt("score_get_num");
        if (num<=0 || num>10000) {
            throw new RRException("系统异常");
        }

        //投入不足
        double limit = appConfigService.getInt("score_get_limit");
        BigDecimal allPay = userDailyLuckRecordService.getPayToday(userId);

        if (allPay==null || allPay.doubleValue()<limit) {
            throw new RRException("未达到领取条件");
        }

        //钱包变动
        wallet.setScore(BigDecimal.valueOf(num));
        walletService.updateWithLock(wallet);

        //积分领取记录
        UserGetScoreRecord record = new UserGetScoreRecord();
        record.setUserId(user.getId());
        record.setDevice(user.getDevice());
        record.setIp(user.getIp());
        record.setMobile(user.getMobile());
        record.setRealname(identify.getRealName());
        record.setIdno(identify.getIdNumber());
        record.setScore((int) num);
        save(record);

        //钱包变动记录
        WalletRecord walletRecord = new WalletRecord();
        walletRecord.setUserId(user.getId());
        walletRecord.setBeforeBalance(BigDecimal.ZERO);
        walletRecord.setAfterBalance(new BigDecimal(num));
        walletRecord.setAddType("add");
        walletRecord.setCoinType("score");
        walletRecord.setOperationType("got");
        walletRecord.setChangeBalance(new BigDecimal(num));
        walletRecordService.save(walletRecord);
    }

    //今天领取人数
    private int getNumToday() {
        return getBaseMapper().getNumToday();
    }
}